<template>
<div>
    <RadioGroup v-model="body.t" type="button" @on-change="fRadio">
        <Radio label=1>我是买家</Radio>
        <Radio label=2>我是卖家</Radio>
        <Radio label=3>其他问题</Radio>
        <Radio label=4>隐藏类目</Radio> 
    </RadioGroup><Button type="primary" @click="add">添加</Button>
    <Table border :columns="columns" :data="data"></Table>
</div>
</template>
<script>
import util from '../libs/util';
    export default {

        created() {
            this.initFormatter(); 
            this.load_data(this.body);
            this.columns = [
                    {
                        title: 'ID',
                        key: 'id',
                    },
                    {
                        title: '问题名称',
                        key: 'title',
                    },
                    {
                        title: '创建时间',
                        key: 'create_time',
                        render: function (h, params) {
                            return h('div', 
                               new Date(params.row.create_time*1000).Format('yyyy-MM-dd mm:ss')
                            );
                        }
                    },
                    {
                        title: '更新时间',
                        key: 'update_time',
                        render: function (h, params) {
                            return h('div', 
                               new Date(params.row.update_time*1000).Format('yyyy-MM-dd mm:ss')
                            );
                        }
                    },
                    {
                        title: '操作',
                        key: 'action',
                        width: 150,
                        align: 'center',
                        render: (h, params) => {
                            return h('div', [
                                h('Button', {
                                    props: {
                                        type: 'primary',
                                        size: 'small'
                                    },
                                    style: {
                                        marginRight: '5px'
                                    },
                                    on: {
                                        click: () => {
                                            this.del(params.index)
                                        }
                                    }
                                }, '删除'),
                                h('Button', {
                                    props: {
                                        type: 'primary',
                                        size: 'small'
                                    },
                                    style: {
                                        marginRight: '5px'
                                    },
                                    on: {
                                        click: () => {
                                            this.show(params.index)
                                        }
                                    }
                                }, '编辑'),
                            ]);
                        }
                    }
                ];
          
        },
        data () {
            return {
                columns: [],
                data: [],
                body: {status:1, t:1},
            }
        },
        methods: {
            fRadio() {
               this.load_data();
            },

            load_data() {
                this.$http.post(util.baseURL+"help/helpList",this.body,
                    {emulateJSON: true})
                    .then( 
                        (response)=>{
                            if(response.data.data) {
                              this.data = response.data.data;
                            } else {
                               this.$Message.error("暂无问题列表！");
                            }
                        },
                        (error)=>{
                            console.log(error);
                        }
                );
            },
            
            // 显示编辑 
            show (index) {
                var query = {help_id : this.data[index].id};
                this.$router.push({path:"/index/editHelp",query: query});
            },

            // 删除
            del (index) {
                var help_id = this.data[index].id;
                this.$http.post(util.baseURL+"help/delHelpInfo",{id: help_id},
                    {emulateJSON: true})
                    .then(
                        (response)=>{
                            if(response.data.data) {
                                console.log(this.info);
                                this.data.splice(index,1);
                              this.$Message.info("删除成功");
                            } else {
                               this.$Message.error("删除失败");
                            }
                        },
                        (error)=>{
                            console.log(error);
                        }
                );
            },

            // 添加
            add() {
                this.$router.push({path:"/index/addHelp"});
            },
            
            initFormatter(){
                Date.prototype.Format = function(fmt) { //
                    let o = {  
                        "M+" : this.getMonth()+1,                 //月份  
                        "d+" : this.getDate(),                    //日  
                        "h+" : this.getHours(),                   //小时  
                        "m+" : this.getMinutes(),                 //分  
                        "s+" : this.getSeconds(),                 //秒  
                        "q+" : Math.floor((this.getMonth()+3)/3), //季度  
                        "S"  : this.getMilliseconds()             //毫秒  
                    };  
                    if(/(y+)/.test(fmt))  
                        fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));  
                    for(var k in o)  
                        if(new RegExp("("+ k +")").test(fmt))  
                            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));  
                    return fmt;  
                }
            }
        }
    }
</script>
